#! /usr/bin/perl
# $OpenBSD: mksqlitedb,v 1.46 2013/11/01 14:50:29 espie Exp $
#
# Copyright (c) 2006-2010 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# example script that shows how to store all variable values into a
# database, using SQLite for that purpose.
#
# usage: cd /usr/ports && mksqlitedb

use strict;
use warnings;
use FindBin;
use lib $FindBin::Bin;
use Getopt::Std;
use Column;
use Var;
use Inserter;
use DBI;
use PkgPath;
use Info;
use TreeWalker;

package MyTreeWalker;
our @ISA = (qw(TreeWalker));

sub new
{
	my ($class, $inserter) = @_;
	bless { inserter => $inserter }, $class;
}

sub inserter
{
	return shift->{inserter};
}

sub handle_value
{
	my ($self, $o, $var, $value, $arch) = @_;
	$o->{info} //= Info->new($o);
	$o->{info}->create($var, $value, $arch, $o);
}

sub parse_dump
{
	my ($self, $fd, $subdirs) = @_;
	$self->SUPER::parse_dump($fd, $subdirs);
	$self->inserter->commit_to_db;
}

sub create_missing_vars
{
	my ($self, $o) = @_;
	for my $name (qw(SHARED_LIBS TARGETS)) {
		if (!defined $o->{info}->{vars}{$name}) {
			$o->{info}->create($name, '', undef, $o);
		}
	}
}

sub handle_path
{
	my ($self, $pkgpath) = @_;
	$self->create_missing_vars($pkgpath);
	my $key = $pkgpath->fullpkgpath;
	if ($pkgpath->{info}{done}) {
		print "--- $key (already done)\n";
		return;
	}
	print "+++ $key\n";
	$self->inserter->set_newkey($key);
	for my $var ($pkgpath->{info}->variables) {
		$self->inserter->add_var($var);
	}
	$pkgpath->{info}->reclaim;
	$pkgpath->{info}{done} = 1;
	$pkgpath->{info}{canonical} = $pkgpath;
	$pkgpath->{done} = 1;
	$self->inserter->finish_port;
}

package main;

our ($opt_v, $opt_q, $opt_p);

getopts('vq:p:');
my $dbname;
if (@ARGV > 0) {
	$dbname = shift;
} else {
	$dbname = 'sqlports';
}

my $inserter = InserterList->new;

my $db = DBI->connect("dbi:SQLite:dbname=$dbname", '', '', {AutoCommit => 0});
my $db2 = DBI->connect("dbi:SQLite:dbname=$dbname-compact", '', '',
    {AutoCommit => 0});
my $normal = NormalInserter->new($db, 1000, $opt_v);
$inserter->add($normal, CompactInserter->new($db2, 1000, $opt_v));


$inserter->create_tables($Info::vars);

if ($opt_p) {
	$ENV{'REPORT_PROBLEM_LOGFILE'}= $opt_p;
}

my $walker = MyTreeWalker->new($inserter);
$walker->dump_all_dirs;

print "Aliases\n";
for my $v (PkgPath->seen) {
	next unless defined $v->{needalias};
	my $alias = $v->{info}{canonical};
	if (defined $alias) {
		print $v->fullpkgpath, "->", $alias->fullpkgpath, "\n";
		$walker->inserter->add_path($v->fullpkgpath, $alias->fullpkgpath);
	} else {
		print "!!! Can't figure out alias for ", $v->fullpkgpath, "\n";
	}
}

$walker->inserter->commit_to_db;

while (my ($k, $v) = each %$Info::unknown) {
	print STDERR "Unknown variable $k in ", $v->fullpkgpath, "\n";
}

if (defined $opt_q) {
	open(my $log, ">", $opt_q) or die $!;
	$walker->inserter->write_log($log);
} else {
	$walker->inserter->write_log(\*STDERR);
}
